<?php

namespace app\api\controller;

use app\admin\model\Coupon;
use app\admin\model\Usercoupon;
use app\common\controller\Api;
use app\admin\model\Order;
class Profile extends Api
{
    protected $noNeedLogin =[];
    protected $noNeedRight = '*';
    //用户信息
    public function getUserInfo()
    {
        $this->success('suc');
    }
    /**
     * 我的订单
     * @param mixed $page
     * @return void
     */
    public function order($page = 1)
    {
        $list = Order::where(['user_id' => $this->auth->id])->paginate(20,false,['page' => $page]);
        $this->success('suc',$list);
    }
    /**
     * 
     * 订单详情
     * @param mixed $id
     * @return void
     */
    public function orderDetail($id)
    {
        $orderDetail = Order::find($id);
        $this->success('suc',$orderDetail);
    }
    /**
     ** 下单
     * Summary of addOrder
     * @return void
     */
    public function addOrder()
    {
        $inserData['user_id'] = $this->auth->id;
        $inserData['create_time'] = time();
        $inserData['order_id'] = Order::generateOrderSn();
        $inserData['address_id'] = $this->request->post('addressId');//地址
        $couponId = $this->request->post('couponId',0);
        if($couponId) {//如果有值
            $couponDetail = Coupon::find($couponId);
            if(empty($couponDetail )) $this->error('优惠券不存在');
            if($couponDetail['expired_time'] < time()) $this->error('优惠券已过期');
            $inserData['coupon_id'] =  $couponId;
            $inserData['coupon_name'] =  $couponDetail['name'];
            $inserData['coupon_money'] =  $couponDetail['money'];
        }
        Order::insert($inserData);
        $this->success('suc');
    }
    /**
     * 领取优惠券
     * Summary of addCoupon
     * @param mixed $couponId
     * @return void
     */
    public function addCoupon($couponId = '')
    {
        $couponDetail = Coupon::find($couponId);
        if(empty($couponDetail )) $this->error('优惠券不存在');
        if($couponDetail['expired_time'] < time()) $this->error('优惠券已过期');
        $inserData['coupon_id'] =  $couponId;
        $inserData['coupon_name'] =  $couponDetail['name'];
        $inserData['money'] =  $couponDetail['money'];
        $inserData['user_id'] = $this->auth->id;
        $inserData['expired_time'] = $couponDetail['expired_time'];
        Usercoupon::insert( $inserData);
        $this->success('suc');
    }
    /**
     * 我的优惠券
     * Summary of coupon
     * @param mixed $page
     * @return void
     */
    public function coupon($page = 1,$status)
    {
       $list = Usercoupon::where(['user_id' => $this->auth->id])->paginate(20,false,['page' => $page]);
       $list->each(function($item){
           if($item['order_id']) {//如果有
                $item['status_text'] = '已使用';
           } else {
                if($item['expired_time'] < time()) {
                    $item['status_text'] = '已过期';
                }else {
                    $item['status_text'] = '待使用';
                }
           }
       });
       $this->success('suc',$list);
    }
    /**
     * 同意或者拒绝
     * 8=拒绝
     * 9=同意
     * Summary of refuse
     * @param mixed $id
     * @param mixed $status
     * @return void
     */
    public function refuse($id,$status = 8)
    {
        Order::where(['id' => $id])->update(['status' => $status]);
        $this->error('suc');
    }


}
